package com.yongninger.activity.user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.yongninger.activity.R;
import com.yongninger.dao.UserDao;
import com.yongninger.vo.UserVo;

public class ViewActivity extends Activity {
	private UserDao userDao;
	private EditText uname;
	private EditText upwd;
	private CheckBox umenu11;
	private CheckBox umenu12;
	private CheckBox umenu21;
	private CheckBox umenu22;
	private CheckBox umenu31;
	private CheckBox umenu32;
	private CheckBox umenu41;
	private CheckBox umenu42;
	private CheckBox umenu51;
	private CheckBox umenu52;
	private CheckBox umenu61;
	private CheckBox umenu62;
	UserVo userVo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_view);
		userDao = new UserDao(getApplicationContext());
		Intent intent = getIntent();
		userVo = (UserVo) intent
				.getSerializableExtra("userVo");
		uname = (EditText)findViewById(R.id.uname);
		uname.setText(userVo.getUname());
		upwd = (EditText)findViewById(R.id.upwd);
		upwd.setText(userVo.getUpwd());

		umenu11 = (CheckBox)findViewById(R.id.umenu11);
		if("0".equals(userVo.getUmenu11())){
			umenu11.setChecked(false);
		}else{
			umenu11.setChecked(true);
		}
		umenu12 = (CheckBox)findViewById(R.id.umenu12);
		if("0".equals(userVo.getUmenu12())){
			umenu12.setChecked(false);
		}else{
			umenu12.setChecked(true);
		}

		umenu21 = (CheckBox)findViewById(R.id.umenu21);
		if("0".equals(userVo.getUmenu21())){
			umenu21.setChecked(false);
		}else{
			umenu21.setChecked(true);
		}
		umenu22 = (CheckBox)findViewById(R.id.umenu22);
		if("0".equals(userVo.getUmenu22())){
			umenu22.setChecked(false);
		}else{
			umenu22.setChecked(true);
		}

		umenu31 = (CheckBox)findViewById(R.id.umenu31);
		if("0".equals(userVo.getUmenu31())){
			umenu31.setChecked(false);
		}else{
			umenu31.setChecked(true);
		}
		umenu32 = (CheckBox)findViewById(R.id.umenu32);
		if("0".equals(userVo.getUmenu32())){
			umenu32.setChecked(false);
		}else{
			umenu32.setChecked(true);
		}

		umenu41 = (CheckBox)findViewById(R.id.umenu41);
		if("0".equals(userVo.getUmenu41())){
			umenu41.setChecked(false);
		}else{
			umenu41.setChecked(true);
		}
		umenu42 = (CheckBox)findViewById(R.id.umenu42);
		if("0".equals(userVo.getUmenu42())){
			umenu42.setChecked(false);
		}else{
			umenu42.setChecked(true);
		}

		umenu51 = (CheckBox)findViewById(R.id.umenu51);
		if("0".equals(userVo.getUmenu51())){
			umenu51.setChecked(false);
		}else{
			umenu51.setChecked(true);
		}
		umenu52 = (CheckBox)findViewById(R.id.umenu52);
		if("0".equals(userVo.getUmenu52())){
			umenu52.setChecked(false);
		}else{
			umenu52.setChecked(true);
		}

		umenu61 = (CheckBox)findViewById(R.id.umenu61);
		if("0".equals(userVo.getUmenu61())){
			umenu61.setChecked(false);
		}else{
			umenu61.setChecked(true);
		}
		umenu62 = (CheckBox)findViewById(R.id.umenu62);
		if("0".equals(userVo.getUmenu62())){
			umenu62.setChecked(false);
		}else{
			umenu62.setChecked(true);
		}
		umenu12.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu11.setChecked(true);
				}
			}
		});
		umenu22.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu21.setChecked(true);
				}
			}
		});
		umenu32.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu31.setChecked(true);
				}
			}
		});
		umenu42.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu41.setChecked(true);
				}
			}
		});
		umenu52.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu51.setChecked(true);
				}
			}
		});

		umenu62.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					umenu61.setChecked(true);
				}
			}
		});
		umenu11.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu12.setChecked(false);
				}
			}
		});
		umenu21.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu22.setChecked(false);
				}
			}
		});
		umenu31.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu32.setChecked(false);
				}
			}
		});
		umenu41.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu42.setChecked(false);
				}
			}
		});
		umenu51.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu52.setChecked(false);
				}
			}
		});
		umenu61.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (!isChecked) {
					umenu62.setChecked(false);
				}
			}
		});
		Button modifyBtn = (Button) this.findViewById(R.id.modify);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (update()) {
					Toast.makeText(getApplicationContext(), "修改成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "修改失败", 2).show();
				}
			}
		};
		modifyBtn.setOnClickListener(onClickListener);

		/*
		 * 删除按钮
		 */
		Button deleteBtn = (Button) this.findViewById(R.id.delete);
		OnClickListener onClickListener1 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 删除
				boolean result = userDao.deleteUserVo(userVo.getUid());
				if (result) {
					Toast.makeText(getApplicationContext(), "删除成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "删除失败", 2).show();
				}
			}
		};
		deleteBtn.setOnClickListener(onClickListener1);
	}

	private boolean update() {
		EditText uname = (EditText) findViewById(R.id.uname);
		EditText upwd = (EditText) findViewById(R.id.upwd);
		UserVo userVoTemp = new UserVo();
		userVoTemp.setUname(uname.getText().toString());
		userVoTemp.setUpwd(upwd.getText().toString());
		userVoTemp.setUtype("1");
		userVoTemp.setUmenu1("1发货单");
		
		if(umenu11.isChecked()){
			userVoTemp.setUmenu11("1");
		}else{
			userVoTemp.setUmenu11("0");
		}
		if(umenu12.isChecked()){
			userVoTemp.setUmenu12("1");
		}else{
			userVoTemp.setUmenu12("0");
		}
		
		if(umenu21.isChecked()){
			userVoTemp.setUmenu21("1");
		}else{
			userVoTemp.setUmenu21("0");
		}
		if(umenu22.isChecked()){
			userVoTemp.setUmenu22("1");
		}else{
			userVoTemp.setUmenu22("0");
		}
		
		if(umenu31.isChecked()){
			userVoTemp.setUmenu31("1");
		}else{
			userVoTemp.setUmenu31("0");
		}
		if(umenu32.isChecked()){
			userVoTemp.setUmenu32("1");
		}else{
			userVoTemp.setUmenu32("0");
		}
		
		if(umenu41.isChecked()){
			userVoTemp.setUmenu41("1");
		}else{
			userVoTemp.setUmenu41("0");
		}
		if(umenu42.isChecked()){
			userVoTemp.setUmenu42("1");
		}else{
			userVoTemp.setUmenu42("0");
		}
		
		if(umenu51.isChecked()){
			userVoTemp.setUmenu51("1");
		}else{
			userVoTemp.setUmenu51("0");
		}
		if(umenu52.isChecked()){
			userVoTemp.setUmenu52("1");
		}else{
			userVoTemp.setUmenu52("0");
		}

		if(umenu61.isChecked()){
			userVoTemp.setUmenu61("1");
		}else{
			userVoTemp.setUmenu61("0");
		}
		if(umenu62.isChecked()){
			userVoTemp.setUmenu62("1");
		}else{
			userVoTemp.setUmenu62("0");
		}
		return userDao.updateUserVo(userVoTemp, userVo.getUid());
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Toast.makeText(getApplicationContext(), "请先保存内容",
					Toast.LENGTH_SHORT).show();
			return false;
		}else if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() >= 0){
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(ViewActivity.this,
					ListActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
